
public class SortedSquares {
    public int[] sortedSquares(int[] nums) {
        int[] numRes = new int[nums.length];
        int j = nums.length-1;
        int k = nums.length-1;
        int i = 0;
        while (i<=j){
            if(nums[i]*nums[i] >= nums[j]*nums[j]){
                numRes[k--] = nums[i]*nums[i];
                i++;
            }else {
                numRes[k--] = nums[j]*nums[j];
                j--;
            }
        }

        return numRes;
    }
}
